package com.googeioextended.youtubebetter;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

//import com.google.gdata.client.youtube.YouTubeQuery;
//import com.google.gdata.client.youtube.YouTubeService;
//import com.google.gdata.data.youtube.VideoEntry;
//import com.google.gdata.data.youtube.VideoFeed;
//import com.google.gdata.util.ServiceException;

public class SearchableActivity extends Activity {
	
	public String TAG = "SearchableActivity";
	public static String SEARCH_URL = "http://gdata.youtube.com/feeds/mobile/videos?alt=json&q=queryString";
	private static String APP_NAME = "tombennett-youtube-0";
	private static String CLIENT_ID = "AI39si6ILnyIIMLQGHwU4TgEewYNdrZQ2HJlrd3ITqc2Jv-IKx7X-jyQ99pdn0-lHoBfRJii2d4PPr5mcRCd6GD1PckHSjKKLA";

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.search);

	    // Get the intent, verify the action and get the query
	    Intent intent = getIntent();
	    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
	      String query = intent.getStringExtra(SearchManager.QUERY);
	      try {
			doMySearch(query);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    }
	}
	
	private void doMySearch(String queryString) throws MalformedURLException {
//		YouTubeService service = new YouTubeService(APP_NAME, CLIENT_ID);
//		YouTubeQuery query = new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/mobile/videos"));
//		// order results by the number of views (most viewed first)
//		query.setOrderBy(YouTubeQuery.OrderBy.VIEW_COUNT);
//		
//		//- TODO: set caption to be true
//		// http://code.google.com/apis/youtube/2.0/reference.html#captionsp
//		//query.setCaption(true);
//
//		query.setFullTextQuery(URLEncoder.encode(queryString));
////		query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);
//
//		Log.d(TAG, "Starting search with url: " + query.getUrl());
//		try {
//			VideoFeed videoFeed = service.query(query, VideoFeed.class);
//			
//			Log.d(TAG, videoFeed.toString()); 
//			List<VideoEntry> videos = videoFeed.getEntries();
//			for (VideoEntry entry : videos) {
//				Log.d(TAG, entry.toString());
//			}
//
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ServiceException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		//printVideoFeed(videoFeed, true);
		
	}
}
